<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        let id = Symbol("id");

        let person = {
            [id]: '',
            name: '',
            class: c = {
                major: '',
                classNumber: ''
            },

            study: () => {
                alert("正在学习");
                for (let i in this) {
                    console.log(i);
                }
            },

            // 改成了访问器属性👇

            set setName(name) {
                console.log('set name');
                for (let char in name)
                    if (/^[\u4e00-\u9fa5]$/.test(name[char])) {
                        alert('名字不应当是中文')
                        return;
                    }
                if (name.length < 4) {
                    alert('名字长度太短');
                    return;
                }
                this.name = name;
            },

            get getName() {
                console.log('get name');
                return this.name;
            },

            set setClass(newClass) {
                console.log('set class');
                let result = true;
                if (Number.isNaN(newClass.classNumber / 1)) {
                    result = false;
                    alert('classNumber必须是数字');
                }
                for (let char in newClass.major) {
                    if (!/^[\u4e00-\u9fa5]$/.test(newClass.major[char])) {
                        result = false;
                        alert('major必须是中文');
                    }
                }
                if (result) this.class = newClass;
            },

            get getClass() {
                console.log('get class');
                return this.class;
            }
        }


        console.log(person)
        person.setName = 'zky'
        console.log(person)
        person.setClass = {
            major: 'z',
            classNumber: 'p'
        }
        console.log(person)
    </script>
</head>
<body>
<div id="root"></div>
</body>
</html>